﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BartonExtension
{
    public static class ConvertExtensions
    {
        public static T toEnum<T>(this object enumObj)
        {
            return (T)Enum.Parse(typeof(T),enumObj.ToString());
        }
        public static DateTime? toDateTime(this string formatString)
        {
            if (formatString.isNullOrEmpty())
                return null;
            else
                return Convert.ToDateTime(formatString);
        }

        public static Int64? toInt64(this object intObject)
        {
            if (intObject == null)
            {
                return null;
            }
            else
                return Convert.ToInt64(intObject);
        }

        public static Int32? toInt32(this object intObject)
        {
            if (intObject == null)
            {
                return null;
            }
            else
                return Convert.ToInt32(intObject);
        }

        public static double? toDouble(this object doubleObject)
        {
            if (doubleObject == null)
            {
                return null;
            }
            else
                return Convert.ToDouble(doubleObject);
        }

        public static float? toFloat(this object floatObject)
        {
            if (floatObject == null)
            {
                return null;
            }
            else
                return (float)Convert.ToDouble(floatObject);
        }

        public static bool? toBoolean(this object boolObject)
        {
            if (boolObject == null)
            {
                return null;
            }
            else
                return (bool)Convert.ToBoolean(boolObject);
        }
        public static string fromBase64(this string base64Str)
        {
            byte[] bf = Convert.FromBase64String(base64Str);
            return Encoding.UTF8.GetString(bf);
        }
        public static string toBase64(this string str)
        {
            byte[] bf = Encoding.UTF8.GetBytes(str);
            return Convert.ToBase64String(bf);
        }
    }
}
